package com.oocl.euc.core.maintenance.controller;

import com.oocl.euc.common.utils.UserContextHolder;
import com.oocl.euc.core.maintenance.model.Function;
import com.oocl.euc.core.maintenance.service.FunctionService;
import com.oocl.euc.core.maintenance.vo.FunctionVO;
import com.oocl.euc.framework.base.controller.RESTController;
import com.oocl.euc.framework.base.service.BaseService;
import com.oocl.euc.framework.common.utils.ResponseEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rest/function")
public class FunctionController extends RESTController<Function, Long> {

    @Autowired
    private FunctionService functionService;

    @Override
    protected BaseService<Function, Long> getBaseService() {
        return functionService;
    }

    @RequestMapping("/findFunctionListAuth")
    public ResponseEntity findByFunctionId(@RequestParam(name = "functionId") Long functionId) {
        return ResponseEntityUtil.successInRestful(functionService.findByFunctionId(functionId));
    }

    @RequestMapping("/getFunctionList")
    public ResponseEntity obtainFunctionList(@RequestParam(value = "pcrsNum", required = true) String pcrsNum) {
        List<FunctionVO> functionVOS = functionService.obtainFunctionList(UserContextHolder.getUserContext().getUserInfo().getId(), pcrsNum);
        List<String> functions = new ArrayList<>();
        functionVOS.stream().map(functionVO -> functions.add(functionVO.getFunctionName())).collect(Collectors.toList());
        return ResponseEntityUtil.successInRestful(functions);
    }
}
